#include <iostream>
#include <fstream>

#include "dist.h"

int main(int argc, char** argv) {
    using namespace std;
    using namespace simi;

    vector<string> files;
    files.reserve(argc - 1);

    for (int i = 1; i < argc; ++i) {
        files.push_back(argv[i]);
    }

    // On normalize
    normalize(files);

    // On initialise la matrice
    double** mat = new double*[files.size()];
    for (int i = 0; i < files.size(); ++i) {
        mat[i] = new double[files.size()];
        for (int j = 0; j < files.size(); ++j) {
            mat[i][j] = -1;
        }
    }

    // On calcule les distances
    cout << "On calcul les distances des images deux par deux." << endl;
    for (int i = 0; i < files.size(); ++i) {
        for (int j = 0; j < files.size(); ++j) {
            if (mat[i][j] == -1) {
                mat[i][j] = simi::distance(files[i] + ".norm",
                        files[j] + ".norm");
                mat[j][i] = mat[i][j];
                if (i == j) {
                    cout << "Distance de l'image " << i << " = " << mat[j][i] << endl;
                }
            }
        }
    }

    cout << "Affichage de mat :"<<endl;
    for (int i = 0; i < files.size(); ++i) {
        for (int j = 0; j < files.size(); ++j) {
            cout << mat[i][j] << " , " ;
        }
        cout << endl;
    }

    double** matNorm = new double*[files.size()];
    for (int i = 0; i < files.size(); ++i) {
        matNorm[i] = new double[files.size()];
        for (int j = 0; j < files.size(); ++j) {
            matNorm[i][j] = 1;
        }
    }

    // On normalise la matrice
    for (int i = 0; i < files.size(); ++i) {
        for (int j = 0; j < files.size(); ++j) {
            //if(i != j && mat[i][j] > 1) {
            if (i != j) {
                matNorm[i][j] = mat[i][j] / min(mat[i][i], mat[j][j]);
                //matNorm[j][i] = mat[i][j];
            }
        }
        matNorm[i][i] = 1;
    }

    // On affiche la matrice des distances
    ofstream csv("matrice.csv");
    cout << "Matrice normalisé final : " << endl;
    for (int i = 0; i < files.size(); ++i) {
        for (int j = 0; j < files.size(); ++j) {
            csv << matNorm[i][j] << ',';
            cout << matNorm[i][j] << ' ';
        }
        cout << endl;
        csv << endl;
    }
    csv.close();

    return 0;
}
